<?php
namespace Crawl\Controller;
use Common\Controller\AdminbaseController;
use Think\Cache\Driver\Redis;
// +----------------------------------------------------------------------
// | Date:2018年1月22日
// +----------------------------------------------------------------------
// | Author: EK_熊<1439527494@qq.com>
// +----------------------------------------------------------------------
// | Description: 此文件作用于****
// +----------------------------------------------------------------------
class EmailAddressController extends AdminbaseController{
    protected $model;
    protected $redis_key;
    public function _initialize(){
        $this->model = M('CrawlEmailAddress');
        $this->redis_key = C('REDIS_PREFIX')."email";
        $this->assign('title','邮箱管理');
        $this->assign('status',['2'=>'关闭','1'=>'正常']);
    }
    
    public function index(){
        $list = $this->lists($this->model);
        $this->assign('list',$list);
        $this->display();
    }
    
    
    public function flush(){
        $oldkey = $this->model->where(['status'=>1])->select();
        $redis = new Redis();
        foreach ($oldkey as $k=>$v){
            $ret_redis = $redis->hSet($this->redis_key,$v['id'],$v['email']);
        }
        $this->success('缓存更新完成！~');
    }
    
    public function edit(){
        $id = I('id');
        $pk = $this->model->getPk();
        if (IS_POST){
            $redis = new Redis();
            $post = I('post');
            $post['update_time'] =CURRENT_TIME;
            $where[$pk] = $id;
            
            $oldkey = $this->model->where($where)->getField('email');
            $ret = $this->model->where($where)->save($post);
            if (!$ret) $this->error('操作失败！');
            $ret_redis = $redis->hSet($this->redis_key,$id,$post['email']);
                
            $this->success('操作成功！');
        }
        $info = $this->model->where([$pk=>$id])->find();
        $this->assign('status',$this->status);
        $this->assign('frequency',['1'=>'实时接收','2'=>'定时接收[每周5下午6点发送]']);
        $this->assign('info',$info);
        $this->display();    
    }
    
    public function add(){
        if (IS_POST) {
            
            
            $redis = new Redis();
            
            $post = I('post');
            $post['update_time'] =CURRENT_TIME;
            $post['create_time'] =CURRENT_TIME;
            $exist = $this->model->where(["email"=>$post['email']])->find();
            if ($exist) $this->error("该邮箱已经存在");
             
            $ret = $this->model->add($post);
            if (!$ret) $this->error('添加失败！');
             
            $ret_redis = $redis->hSet($this->redis_key,$ret,$post['email']);
            
            $this->success('添加成功！');
        }
        $this->assign('frequency',['1'=>'实时接收','2'=>'定时接收[每周5下午6点发送]']);
        $this->display();       
    }
    

    /**
     * 修改单条记录状态
     * @param unknown $model
     * @param array $val
     * date:2016年11月22日
     * author: EK_熊
     */
    public function setStatus(){
        $model = $this->model;
        $id = I('id');
        $status = I('status');
        $pk = $model->getPk();
    
        $ret = $model->where([$pk=>$id])->save(['status'=>$status,'updated_time'=>CURRENT_TIME]);
        if (!$ret) $this->error('操作失败,网络异常！');
        $redis = new Redis();
        
        if ($status == 2 ){
            $redis->hDel($this->redis_key,$id);
        }else{
            $email = $this->model->where(['id'=>$id])->getField('email');
            $redis->hSet($this->redis_key,$id,$email);
        }
        
        $this->success('操作成功！');
    }
    
    /**
     * 删除数据,可批量
     * @param unknown $model
     * date:2016年11月22日
     * author: EK_熊
     */
    public function delete($model=''){
        $model = empty($model) ? $this->model : $model;
        $ids = I('ids');
        $pk = $model->getPk();
        $map[$pk] = array('in',$ids);
        $ret = $model->where($map)->delete();
        if (!$ret) $this->error('操作失败,网络异常！');
        $redis = new Redis();
        foreach ($ids as $v) {
            $redis->hDel($this->redis_key,$v);
        }
        $this->success('操作成功！');
    }    
    
}